package com.wl.user.login.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wl.entity.Follow;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Map;

@Mapper
public interface FollowMapper extends BaseMapper<Follow> {
    // 自定义 INSERT
    @Insert("INSERT INTO follow (follower_id, following_id, status, time) " +
            "VALUES (#{followerId}, #{followingId}, #{status}, #{time})")
    int insertFollow(Follow follow);

    // 插入时避免重复（如果已存在则更新状态）
    @Insert("INSERT INTO follow (follower_id, following_id, status, time) " +
            "VALUES (#{followerId}, #{followingId}, #{status}, #{time}) " +
            "ON DUPLICATE KEY UPDATE status = #{status}, time = #{time}")
    int insertOrUpdateFollow(Follow follow);

    //更新状态
    @Update("update follow set status = #{status} where follower_id = #{followerId} and following_id = #{followingId}")
    int updateStatus(Integer followerId, Integer followingId,Integer status);

    //检查是否已关注
    @Select("select count(*) from follow where follower_id=#{followerId}" +
            "and following_id=#{followingId} and status = 1")
    boolean existsFollowRelation(Integer followerId, Integer followingId);

    //提示查询用户得关注数和粉丝数
    @Select("SELECT\n" +
            "    (SELECT COUNT(*) FROM follow WHERE follower_id = #{followerId} AND status = 1) AS follower_count,\n" +
            "    (SELECT COUNT(*) FROM follow WHERE following_id = #{followingId} AND status = 1) AS following_count;")
    Map<String, Long> countFollowStats(Integer followerId, Integer followingId);

    //查询被关注者信息
//    @Select("SELECT following_id FROM follow WHERE follower_id = #{userId} AND status = 1")
//    List<Integer> selectFollowingIds(@Param("userId") Integer userId);
//
//    @Select("SELECT follower_id FROM follow WHERE following_id = #{userId} AND status = 1")
//    List<Integer> selectFollowerIds(@Param("userId") Integer userId);
}
